#include "StdAfx.h"
#include "dll_object.h"
#include <interface/interface.h>
#include "multi_lang_impl.h"
#include "session_impl.h"

#pragma warning(disable:4996)

aot::tt::imain*				dll_object::im_ = NULL;
dll_object* g_dll_obj = NULL;

dll_object::dll_object()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	this->im_ = NULL;
	this->ptr_ft_client_ = NULL;
	this->ptr_multi_lang_ = new multi_lang_impl;	/// handled del
}

dll_object::~dll_object()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	if ( this->ptr_ft_client_ )
	{
		delete this->ptr_ft_client_;
		this->ptr_ft_client_ = NULL;
	}

	if( this->ptr_multi_lang_ )
	{
		delete this->ptr_multi_lang_;
		this->ptr_multi_lang_ = NULL;
	}
}

void 
dll_object::destroy()
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	if( 0 == dec_ref() )
		delete this;
}

bool 
dll_object::on_regist(aot::tt::idll::dll_regist_param_t* p, aot::tt::result_t* ret)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	this->parent_wnd_ = p->parent_wnd;

	regist_to_trans_msg(p, ret);

	this->im_ = p->mi;
	this->global_.init(this->im_);
	this->i_wrapper_.init_component_query(this->im_, this);
	this->ptr_multi_lang_->init();

	return true;
}

int	
dll_object::on_putq (aot::tt::message_t* msg, long tm, aot::tt::result_t* ret)
{
	return -1;
}

int	
dll_object::on_command (aot::tt::command_t* cmd, aot::tt::result_t* ret)
{
	return -1;
}

bool 
dll_object::query_interface(void** out, const char* key)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	if( 0 == strcmp("ifile_trans_client", key) ) 
	{
		if ( NULL == this->ptr_ft_client_ )
		{
			this->ptr_ft_client_ = new file_trans_client_impl();/// handled del
		}

		*out = this->ptr_ft_client_;

		return true;
	}

	return false;
}

bool 
dll_object::regist_to_trans_msg(aot::tt::idll::dll_regist_param_t* p, aot::tt::result_t* ret)
{
	aot::tt::imain::regist_trans_msg_t rg_param;
	rg_param.is_trans_msg = true;
	rg_param.token = p->token;
	p->mi->regist_to(&rg_param, aot::tt::imain::e_regist_trans_msg, NULL);

	return true;
}


BOOL dll_object::pre_tans_msg(MSG* ptr_msg)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	return AfxGetApp()->PreTranslateMessage(ptr_msg);
}

global_data_wrapper* get_global()
{
	return &g_dll_obj->global_;
}

interface_wrapper* get_interface()
{
	return &g_dll_obj->i_wrapper_;
}

/// basic check by fan05